// pages/articles/choose/choose.js

const app = getApp();
const model = require('../../../utils/model');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cates: [],
    list:[],
    scrollHeight:0,
    lastId:0,
    canLoadMore:true,
    loadtips:'数据加载中...',
    showLoading:false
  },

  getCates:function(){
    var me = this;
    model.request({
      action:'mwpdClassifyList',
      success: function (res) {
        //console.log(res.data.flash_list);
        me.setData({
          cates:res.data.list,
          id:res.data.list[0]['id']
        });
        me.getList();
      }
    });
  },

  chooseCate:function(e){
    var id = e.currentTarget.dataset.id;
    this.setData({
      id:id,
      list:[],
      lastId:0
    });
    this.getList();
  },

  getList: function () {
    if (!this.data.canLoadMore){
      return;
    }
    this.data.canLoadMore = false;
    var id = this.data.id;
    var me = this;
    model.request({
      action:'mwpdListByClassify',
      data:{
        classid:id,
        last_id:me.data.lastId
      },
      success: function (res) {
        me.data.canLoadMore = true;
  
        if (res.data.list && res.data.list.length) {
          var newList = me.data.list.concat(res.data.list);
          me.setData({
            list:newList,
            lastId:res.data['last_id'],
            showLoading:false
          });
        }
        else {
          me.setData({
            notShowLoadingFlag:true,
            loadtips:'暂无更多数据'
          });
        }
      }
    });
  },
  
  scroll:function () {
    this.setData({
      showLoading:true
    });
  },

  lower: function () {
    this.getList();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      scrollHeight: app.globalData.winHeight - 93
    });
    this.getCates();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})